<?php

namespace app\common\model;

use app\common\model\Base;

/**
 * Class UserSign
 * @package app\common\model
 */
class UserSign extends Base
{
    /**
     * getSignToday
     * @param int $uid
     * @return int
     */
    public function getSignToday(int $uid): int
    {
        //查询今日是否已签
        $s_time = mktime(0, 0, 0);
        $e_time = strtotime('+1 day', $s_time);
        $where = [
            ['uid', '=', $uid],
            ['add_time', 'between', [$s_time, $e_time]],
        ];
        $count = $this->where($where)->count();
        return $count ? 1 : 2;
    }

    /**
     * getSignHistory
     * @param int $uid
     * @return array
     * @throws
     */
    public function getSignHistory(int $uid): array
    {
        $limit = 5;
        $list = $this->where(['uid' => $uid])->limit($limit)->order('id','desc')->select();
        $list = $list ? $list->toArray() : [];
        foreach ($list as $key => $val)
        {
            $list[$key]['type'] = $val['type'] == 1 ? '日常签到' : '连续签到';
            $list[$key]['time'] = date('Y-m-d H:i:s', $val['add_time']);
        }
        return $list;
    }
}